﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using Com.Crc.PoJo.Model.Resource;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model;
namespace Com.Crc
{
    public class InitControlHelp
    {

        public static String getErrorInfo(int msgCode, string msgInfo)
        {
            Com.Crc.PoJo.Model.ErrorInfo ei = new Com.Crc.PoJo.Model.ErrorInfo();
            ei.msgCode = msgCode;
            ei.msgInfo = msgInfo;

            Jayrock.Json.JsonTextWriter writer = new Jayrock.Json.JsonTextWriter();
            Jayrock.Json.Conversion.JsonConvert.Export(ei, writer);

            string str = writer.ToString();

            return str;
        }

        /// <summary>
        /// 加载局点信息
        /// </summary>
        /// <param name="ddl"></param>
        public static void LoadBranchInfo(DropDownList ddl)
        {
            InitControlManagerBLL bllManager = new InitControlManagerBLL();
            DataTable dtBranch = bllManager.QueryBranchInfo();
            ddl.DataSource = dtBranch;
            ddl.DataTextField = "s_name";
            ddl.DataValueField = "n_id";
            ddl.DataBind();
        }

        /// <summary>
        /// 加载小区信息
        /// </summary>
        /// <param name="ddl"></param>
        public static void LoadSubDistrictInfo(DropDownList ddl)
        {
            InitControlManagerBLL bllManager = new InitControlManagerBLL();
            DataTable dtBranch = bllManager.QuerySubDistrictInfo();
            ddl.DataSource = dtBranch;
            ddl.DataTextField = "s_name";
            ddl.DataValueField = "n_id";
            ddl.DataBind();
        }

        /// <summary>
        /// 加载交换设备信息
        /// </summary>
        /// <param name="ddl"></param>
        public static void LoadExchangeInfo(DropDownList ddl)
        {
            BroadBandManageExchange bllManager = new BroadBandManageExchange();
            DataTable dtExchange = bllManager.QueryExchangeData("", "", "");
            ddl.DataSource = dtExchange;
            ddl.DataTextField = "s_name";
            ddl.DataValueField = "n_id";
            ddl.DataBind();
        }

        /// <summary>
        /// 加载字典表信息
        /// </summary>
        /// <param name="ddl"></param>
        public static void LoadCommonDicInfo(DropDownList ddl, Dictionary.COMMON_DIC_TYPE intType)
        {
            Array arr = Enum.GetValues(intType.GetType());
            InitControlManagerBLL bllManager = new InitControlManagerBLL();
            DataTable dtBranch = bllManager.QueryCommonDicInfo(intType);
            ddl.DataSource = dtBranch;
            ddl.DataTextField = "s_name";
            ddl.DataValueField = "n_id";
            ddl.DataBind();
        }

        public static void SetSelectedValue(DropDownList dropdownList, string selectedValue)
        {
            for (int i = 0; i < dropdownList.Items.Count; i++)
            {
                if (dropdownList.Items[i].Value == selectedValue)
                {
                    dropdownList.SelectedIndex = i;
                    break;
                }
            }
        }

        public static void loadSubDistrict(DropDownList ddl)
        {
            SubDistrictManageBLL bll = new SubDistrictManageBLL();
            bindDataSource(ddl, bll.queryAll(), "s_name", "n_id");
        }

        public static void loadSubdistictUnitInfo(DropDownList ddl)
        {
            SubDistrictManageBLL bll = new SubDistrictManageBLL();
            bindDataSource(ddl, bll.queryAllSubdistictUnitInfo(), "s_name", "n_id");
        }

        public static void loadSubdistictInfo(DropDownList ddl)
        {
            SubDistrictManageBLL bll = new SubDistrictManageBLL();
            bindDataSource(ddl, bll.QuerySubDistrictData(), "s_name", "n_id");
        }

        private static void bindDataSource(DropDownList ddl,DataTable dataTable, String text, String value)
        {
            ddl.DataSource = dataTable;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
        }

        
        internal static void loadONU(DropDownList ddl)
        {
            ONUManageBLL bll = new ONUManageBLL();
            bindDataSource(ddl, bll.QueryONUData("","","",""), "s_name", "n_id");
        }

        internal static void loadArea(DropDownList ddl, Dictionary.COMMON_DIC_TYPE COMMON_DIC_TYPE)
        {
            AREAManagerBLL areabll = new AREAManagerBLL();
            areabll.query(COMMON_DIC_TYPE);
            bindDataSource(ddl, areabll.query(COMMON_DIC_TYPE), "s_name", "n_id");
        }
    }
}